package com.hellobike.haxing.modules.shop.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.util.DateUtils;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.modules.shop.entity.ShopWithdrewMonthCount;
import com.hellobike.haxing.modules.shop.mapper.ShopWithdrewMonthCountMapper;
import com.hellobike.haxing.modules.shop.service.IShopWithdrewMonthCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 每月提现成功总金额，次年清空 服务实现类
 * </p>
 *
 * @author clz
 * @since 2020-02-15
 */
@Service
public class ShopWithdrewMonthCountServiceImpl extends ServiceImpl<ShopWithdrewMonthCountMapper, ShopWithdrewMonthCount> implements IShopWithdrewMonthCountService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void saveMonthAmount(Long shopId, BigDecimal amount) {
        super.baseMapper.saveMonthAmount(amount,shopId,DateUtils.date2Str(new SimpleDateFormat("yyyy-MM")));
        //删除缓存
        redisUtil.del(CacheConstant.WITHDREW_AMOUNT_MONTH_SHOP+shopId);
    }

    @Override
    public void deleteMonthCount() {
        List<ShopWithdrewMonthCount> list = super.baseMapper.queryMonthLtList(DateUtils.date2Str(new SimpleDateFormat("yyyy-MM")));
        for (ShopWithdrewMonthCount shopWithdrewMonthCount:list) {
            baseMapper.deleteMonthCountId(shopWithdrewMonthCount.getId());
            //删除缓存
            redisUtil.del(CacheConstant.WITHDREW_AMOUNT_MONTH_SHOP+shopWithdrewMonthCount.getShopId());
        }
    }
}
